import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';


///  视频播放view
class VideoViewX extends StatefulWidget {
  final String url;

  const VideoViewX({
    super.key,
    required this.url,
  });

  @override
  State<VideoViewX> createState() => _VideoViewXState();
}

class _VideoViewXState extends State<VideoViewX>
    with AutomaticKeepAliveClientMixin {
  late VideoPlayerController videoPlayerController;

  @override
  void initState() {
    super.initState();
    videoPlayerController =
        VideoPlayerController.networkUrl(Uri.parse(widget.url))
          ..initialize().then((_) {
            setState(() {});
          });

    videoPlayerController.addListener(() {
      print(videoPlayerController.value.isPlaying);
    });
  }

  @override
  void dispose() {
    videoPlayerController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      color: Colors.black,
      child: Center(
        child: AspectRatio(
          aspectRatio: videoPlayerController.value.aspectRatio,
          child: SizedBox(
            width: MediaQuery.of(context).size.width,
            child: VideoPlayer(videoPlayerController),
          ),
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
